package com.tamo.ppresta.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.tamo.ppresta.R;
import com.tamo.ppresta.base.BaseActivity;
import com.tamo.ppresta.event.LogOutEvent;
import com.tamo.ppresta.event.UserPhotoEvent;
import com.tamo.ppresta.fragment.AccountFragment;
import com.tamo.ppresta.fragment.BillFragment;
import com.tamo.ppresta.fragment.HomeFragment;
import com.tamo.ppresta.util.LogUtils;
import com.tamo.ppresta.util.MyUtils;
import com.yalantis.ucrop.UCrop;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.fl_content_main)
    LinearLayout mFlContentMain;
    @BindView(R.id.rb_1)
    RadioButton mRb1;
    @BindView(R.id.rb_2)
    RadioButton mRb2;
    @BindView(R.id.rb_3)
    RadioButton mRb3;
    @BindView(R.id.rg)
    RadioGroup mRg;
    @BindView(R.id.activity_radio_group)
    LinearLayout mActivityRadioGroup;

    private FragmentManager fragmentManager;
    private HomeFragment mHomeFragment;
    private BillFragment mBillFragment;
    private AccountFragment mAccountFragment;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void setView() {
        fragmentManager = getSupportFragmentManager();
        //默认选中第一个tab
        showFragment(1);
        mRg.check(R.id.rb_1);
        mRg.setOnCheckedChangeListener(this);
    }

    @Override
    protected void setData() {

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏底部导航栏
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_IMMERSIVE;
        getWindow().setAttributes(params);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    private void showFragment(int page) {
        FragmentTransaction ft = fragmentManager.beginTransaction();

        // 想要显示一个fragment,先隐藏所有fragment，防止重叠
        hideFragments(ft);
        switch (page) {
            case 1:
                // 如果fragment1已经存在则将其显示出来
                if (mHomeFragment != null)
                    ft.show(mHomeFragment);
                    // 否则添加fragment1，注意添加后是会显示出来的，replace方法也是先remove后add
                else {
                    mHomeFragment = new HomeFragment();
                    ft.add(R.id.fl_content_main, mHomeFragment);
                }
                break;
            case 2:
                if (mBillFragment != null)
                    ft.show(mBillFragment);
                else {
                    mBillFragment = new BillFragment();
                    ft.add(R.id.fl_content_main, mBillFragment);
                }
                break;
            case 3:
                if (mAccountFragment != null) {
                    ft.show(mAccountFragment);
                } else {
                    mAccountFragment = new AccountFragment();
                    ft.add(R.id.fl_content_main, mAccountFragment);
                }
                break;
        }
//        ft.commit();

        ft.commitAllowingStateLoss();
    }

    // 当fragment已被实例化，相当于发生过切换，就隐藏起来
    public void hideFragments(FragmentTransaction ft) {
        if (mHomeFragment != null)
            ft.hide(mHomeFragment);
        if (mBillFragment != null)
            ft.hide(mBillFragment);
        if (mAccountFragment != null)
            ft.hide(mAccountFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_1:
                showFragment(1);
                break;
            case R.id.rb_2:
                showFragment(2);
                break;
            case R.id.rb_3:
                showFragment(3);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtils.info(TAG, "requestCode: " + requestCode);
        switch (requestCode) {
            case UCrop.REQUEST_CROP:
                EventBus.getDefault().post(new UserPhotoEvent(data.getData()));
                break;
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (null != mHomeFragment && mHomeFragment.getPopupWindowShow()) {
            LogUtils.info(TAG, "拦截事件");
            return false;
        } else {
            return super.dispatchTouchEvent(event);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void logOutEvent(LogOutEvent logOutEvent) {
        showFragment(1);
        mRg.check(R.id.rb_1);
    }
}